package org.nvframe.component.property;

import java.util.ArrayList;
import java.util.List;

import org.nvframe.component.property.operation.*;

public class TransientPropertyDouble implements TransientProperty {
	
	private double persistentVal;
	private double cachedTransientVal;
	
	private List<Operation> operations;

	public TransientPropertyDouble(float persistentVal) {
		this.persistentVal = persistentVal;
		
		operations = new ArrayList<Operation> ();
		cachedTransientVal = 0;
	}

	private double reCalculateCachedValue() {
		double val = persistentVal;
		
		for(Operation operation : operations)
			val = operation.getResult(val);
		
		cachedTransientVal = val;
		
		return val;
	}
	
	public double getPersistentVal() {
		return persistentVal;
	}
	
	public double getValue() {
		return cachedTransientVal;
	}
	
	public void add(double addVal) {
		operations.add(new Addition(addVal));	
		reCalculateCachedValue();
	}
	
	public void substract(double substractVal) {
		operations.add(new Substraction(substractVal));	
		reCalculateCachedValue();
	}
	
	public void multiply(double multiplyVal) {
		operations.add(new Multiplication(multiplyVal));	
		reCalculateCachedValue();
	}
	
	public void divide(double divideVal) {
		operations.add(new Division(divideVal));	
		reCalculateCachedValue();
	}
	
	public void percentage(double percentage) {
		operations.add(new Percentage(percentage));	
		reCalculateCachedValue();
	}
	
}
